﻿using System;
using System.Windows.Controls;

using SSTU.Contract.Interaction;
using SSTU.Management.Dependency;
using SSTU.Model;
using SSTU.View;
using SSTU.ViewModel.Item;

namespace SSTU.Interaction
{
	/// <summary>
	/// Common interaction with navigation in application.
	/// </summary>
	public class NavigationInteraction : INavigationInteraction
	{
		private readonly Frame _frame;

		/// <summary>
		/// Initializes a new instance of the <see cref="NavigationInteraction"/> class.
		/// </summary>
		/// <param name="frame">The root frame of application.</param>
		public NavigationInteraction(Frame frame)
		{
			DependencyRequirement.Current.NotNullFromConstructorInjection<NavigationInteraction, Frame>(frame, "frame");
			_frame = frame;
		}

		/// <summary>
		/// Navigate to the page with menu for faculty.
		/// </summary>
		/// <param name="faculty">The faculty.</param>
		public void ToFacultyMenu(FacultyItemVM faculty)
		{
			string uriPattern = "/View/FacultyMenuPage.xaml?{0}={1}&{2}={3}&{4}={5}";
			string uriString = string.Format(uriPattern,
				FacultyMenuPage.NavigationParameterNameFacultyId, faculty.Id,
				FacultyMenuPage.NavigationParameterNameFacultyShortName, faculty.ShortName,
				FacultyMenuPage.NavigationParameterNameFacultyLongName, faculty.LongName);
			var uri = new Uri(uriString, UriKind.Relative);
			_frame.Navigate(uri);
		}

		/// <summary>
		/// Navigate to the page with time table for group.
		/// </summary>
		/// <param name="group">The group.</param>
		public void ToGroupTimeTable(GroupItemVM group)
		{
			string uriPattern = "/View/GroupTimeTablePage.xaml?{0}={1}&{2}={3}&{4}={5}";
			string uriString = string.Format(uriPattern,
				GroupTimeTablePage.NavigationParameterNameGroupId, group.Id,
				GroupTimeTablePage.NavigationParameterNameGroupShortName, group.ShortName,
				GroupTimeTablePage.NavigationParameterNameGroupLongName, group.LongName);
			var uri = new Uri(uriString, UriKind.Relative);
			_frame.Navigate(uri);
		}

		/// <summary>
		/// Gets value which indicates that this uri it's uri for concrete group time table.
		/// </summary>
		/// <param name="uri">The Uri.</param>
		/// <param name="groupId">The Group id.</param>
		public bool IsGroupTimeTableUri(Uri uri, string groupId)
		{
			string searchString = string.Format("/View/GroupTimeTablePage.xaml?{0}={1}", GroupTimeTablePage.NavigationParameterNameGroupId, groupId);
			bool result = ((uri != null) && (uri.OriginalString != null) && uri.OriginalString.StartsWith(searchString));
			return result;
		}
	}
}
